package com.cty.binarysearchnorecursion;
/*
*
*
* 二分查找非递归版
* */
public class BinarySearch {
    //数组为升序数组，target为目标值，返回值为目标值下标
    public int binarySearch(int[] arr,int target) {
        int left = 0;
        int right = arr.length - 1;
        int mid;
        while (left <= right) {
            mid = (right + left) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }
}
